/**
 * 
 */
package math;

import exceptions.UnequalLengthException;

/**
 * This class provides static methods for the calculations of vectors.
 * 
 * @author Kevin Wagner
 * @version 1.0
 */
public class VectorCalculations {

	/**
	 * Calculates the sum of the given vectors.
	 * 
	 * @param vector1
	 *            summand 1
	 * @param vector2
	 *            summand 2
	 * @return sum of vector1 and vector2
	 * @throws Exception
	 *             error in vector format
	 */
	public static double[] vectorAddition(double[] vector1, double[] vector2)
			throws Exception {
		double[] result = new double[vector1.length];
		if (vector1.length != vector2.length) {
			throw new UnequalLengthException();
		}
		for (int i = 0; i < result.length; i++) {
			result[i] = vector1[i] + vector2[i];
		}
		return result;
	}

	/**
	 * Calculates the sum of the given vectors.
	 * 
	 * @param vector1
	 *            summand 1
	 * @param vector2
	 *            summand 2
	 * @return sum of vector1 and vector2
	 * @throws Exception
	 *             error in vector format
	 */
	public static int[] vectorAddition(int[] vector1, int[] vector2)
			throws Exception {
		int[] result = new int[vector1.length];
		if (vector1.length != vector2.length) {
			throw new UnequalLengthException();
		}
		for (int i = 0; i < result.length; i++) {
			result[i] = vector1[i] + vector2[i];
		}
		return result;
	}

	/**
	 * Calculates the difference of the given vectors.
	 * 
	 * @param vector1
	 *            minuend
	 * @param vector2
	 *            subtrahend
	 * @return difference of vector1 and vector2
	 * @throws Exception
	 *             error in vector format
	 */
	public static double[] vectorSubtraction(double[] vector1, double[] vector2)
			throws Exception {
		double[] result = new double[vector1.length];
		if (vector1.length != vector2.length) {
			throw new UnequalLengthException();
		}
		for (int i = 0; i < result.length; i++) {
			result[i] = vector1[i] - vector2[i];
		}
		return result;
	}

	/**
	 * Calculates the difference of the given vectors.
	 * 
	 * @param vector1
	 *            minuend
	 * @param vector2
	 *            subtrahend
	 * @return difference of vector1 and vector2
	 * @throws Exception
	 *             error in vector format
	 */
	public static int[] vectorSubtraction(int[] vector1, int[] vector2)
			throws Exception {
		int[] result = new int[vector1.length];
		if (vector1.length != vector2.length) {
			throw new UnequalLengthException();
		}
		for (int i = 0; i < result.length; i++) {
			result[i] = vector1[i] - vector2[i];
		}
		return result;
	}

	/**
	 * Calculates the product of the vector and the number.
	 * 
	 * @param vector
	 *            factor
	 * @param number
	 *            factor
	 * @return product of vector and number
	 */
	public static double[] vectorXNumber(double[] vector, double number) {
		double[] result = new double[vector.length];
		for (int i = 0; i < result.length; i++) {
			result[i] = vector[i] * number;
		}
		return result;
	}

	/**
	 * Calculates the product of the vector and the number.
	 * 
	 * @param vector
	 *            factor
	 * @param number
	 *            factor
	 * @return product of vector and number
	 */
	public static int[] vectorXNumber(int[] vector, int number) {
		int[] result = new int[vector.length];
		for (int i = 0; i < result.length; i++) {
			result[i] = vector[i] * number;
		}
		return result;
	}

	/**
	 * Calculates the product of the vector and the number. The result in every
	 * dimension of the vector will be round to an integer value.
	 * 
	 * @param vector
	 *            factor
	 * @param number
	 *            factor
	 * @return product of vector and number
	 */
	public static int[] vectorXNumber(int[] vector, double number) {
		int[] result = new int[vector.length];
		for (int i = 0; i < result.length; i++) {
			result[i] = (int) Math.round(vector[i] * number);
		}
		return result;
	}
	
	/**
     * @param indices Input index array.
     * @return the indices in inverse order (last is first).
     */
    public static int[] reverse(final int[] indices) {
        int[] reverse = new int[indices.length];
        for (int i = 0; i < indices.length; i++) {
            reverse[i] = indices[indices.length - i - 1];
        }
        return reverse;
    }

}
